<?php

namespace app\logic;

class BankCard
{
    private static $obj = null;

    /**
     * 类入口
     * @return BankCard|null
     */
    public static function getInstance()
    {
        if (is_null(self::$obj))
            self::$obj = new self();
        return self::$obj;
    }

    /**
     * 阿里云市场银行卡信息查询接口
     * @param $bank_card
     * @return array|bool
     * 银行卡类别查询
     * @param string $bank_card 银行卡号
     * @return array|bool
     * @note 判断银行卡真伪、查询类别、logo及客服电话
     *       int error_code 返回码
     *       string reason 返回说明
     *       bank string 银行归属
     *       type string 卡类型
     *       tel string 客服电话
     *       logo string 银行logo，前面需加http://images.juheapi.com/banklogo/
     * {
     *   "reason": "success",
     *   "result": {
     *   "bank": "中国农业银行",
     *   "type": "金穗通宝卡(银联卡)",
     *   "tel": "95599",
     *   "logo": "1067.jpg"
     *   },
     *   "error_code": 0
     *  }
     */
    public function aliYunCheckBankCardType($bank_card)
    {
        $host = "http://api43.market.alicloudapi.com";
        $path = "/api/c43";
        $method = "GET";
        $appcode = config('aliyun.app_code');;
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "bankcard=" . $bank_card;
        $bodys = "";
        $url = $host . $path . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$" . $host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $str_info = curl_exec($curl);
        if (curl_errno($curl)) {
            echo 'Error' . curl_error($curl);
        }
        curl_close($curl);

        $json_info = preg_replace("/.*\r\n/", '', $str_info);
        $arr_info = json_decode($json_info, true);
        if (isset($arr_info['error_code']) && $arr_info['error_code'] == 0) {
            if (!isset($arr_info['result']['bankname'])) return false;
            if (!isset($arr_info['result']['cardname'])) return false;
            if (!isset($arr_info['result']['bankimage'])) return false;

            return [
                'bank' => $arr_info['result']['bankname'],
                'type' => $arr_info['result']['cardname'],
                'logo' => $arr_info['result']['bankimage'],
            ];
        }
        return false;
    }

    /**
     * 阿里云市场四元素校验
     * @param string $real_name 姓名，需要utf8 Urlencode
     * @param string $id_card 身份证号码
     * @param string $bankcard 银行卡卡号
     * @param string $mobile 手机号码
     * @return bool
     * @note 根据姓名、身份证、银行卡、手机号码，检测信息是否匹配
     *    error_code    int    返回码
     *    reason    string    返回码描述
     *    bankcard    string    银行卡卡号
     *    realname    string    姓名
     *    idcard    string    身份证号码
     *    res    int    匹配结果，1:匹配 2:不匹配
     *    jobid    string    本次查询流水号
     *    mobile    string    手机号码
     *    res    string    认证结果，1:匹配 2:不匹配 3:无法认证 4:服务商超时
     *
     * {
     *   "reason": "成功1",
     *   "result": {
     *   "jobid": "2015120913503797592", //本次查询流水号
     *   "realname": "商世界", //姓名
     *   "bankcard": "6259656360701234", //银行卡卡号
     *   "idcard": "310329198103050011", //身份证号码
     *   "mobile": "18912341234", //预留手机号码
     *   "res": "2", //验证结果，1:匹配 2:不匹配 3:无法认证 4:服务商超时
     *   "message": "认证信息不匹配" //描述
     *   },
     *   "error_code": 0
     * }
     */
    public function aliYunVerifyBankCard4($real_name, $id_card, $bankcard, $mobile)
    {
        $host = "http://jisubank4.market.alicloudapi.com";
        $path = "/bankcardverify4/verify";
        $method = "GET";
        $appcode = config('aliyun.app_code');
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "bankcard=" . $bankcard . "&idcard=" . $id_card . "&mobile=" . $mobile . "&realname=" . urlencode($real_name);
        $url = $host . $path . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$" . $host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $str_info = curl_exec($curl);
        if (curl_errno($curl)) {
            echo 'Error' . curl_error($curl);
        }
        curl_close($curl);

        $json_info = preg_replace("/.*\r\n/", '', $str_info);
        $arr_info = json_decode($json_info, true);
        if (isset($arr_info['result']['verifystatus']) && $arr_info['result']['verifystatus'] == 0) {
            return true;
        } else {
            return false;
        }
    }
}
